<%@ page language="java" contentType="text/html; charset=utf-8" %>
<jsp:useBean id="rs" class="weaver.conn.RecordSet" scope="page"/>
<jsp:useBean id="bb" class="weaver.general.BaseBean"/>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="com.alibaba.fastjson.JSONArray" %>
<%@ page import="weaver.conn.RecordSet" %>
<%
    JSONArray jsonArray = new JSONArray();
    String params1 = Util.null2String( request.getParameter( "params1" ) );
    String params2 = Util.null2String( request.getParameter( "params2" ) );
    String type = Util.null2String( request.getParameter( "type" ) );
    if("1".equals( type )){//年度
        rs.executeQuery( "select id,ksdw from uf_gksdbclryb" );
        int index = 1;
        while (rs.next()) {
            JSONObject jsonData = new JSONObject();
            String id = Util.null2String( rs.getString( "id" ) );
            String ksdw = Util.null2String( rs.getString( "ksdw" ) );
            String sqlwhere = " year(sqrq)='"+params1+"' and zrbm='" + id + "'";
            int counts = getCounts( sqlwhere );
            jsonData.put( "index", index );
            jsonData.put( "departmentname", ksdw );
            jsonData.put( "counts", counts );
            jsonArray.add( jsonData );
            index++;
        }
    }else if("2".equals( type )){//季度
        if(!"0".equals(params2)){
            String yearmonth = getDateByQuart(params1,params2);
            rs.executeQuery( "select id,ksdw from uf_gksdbclryb" );
            int index = 1;
            while (rs.next()) {
                JSONObject jsonData = new JSONObject();
                String id = Util.null2String( rs.getString( "id" ) );
                String ksdw = Util.null2String( rs.getString( "ksdw" ) );
                String sqlwhere = " left(sqrq,7) in ("+yearmonth+") and zrbm='" + id + "'";
                int counts = getCounts( sqlwhere );
                jsonData.put( "index", index );
                jsonData.put( "departmentname", ksdw );
                jsonData.put( "quartercount"+params2, counts );
                jsonArray.add( jsonData );
                index++;
            }
        }else {
            String yearmonth1 = getDateByQuart(params1,"1");
            String yearmonth2 = getDateByQuart(params1,"2");
            String yearmonth3 = getDateByQuart(params1,"3");
            String yearmonth4 = getDateByQuart(params1,"4");
            rs.executeQuery( "select id,ksdw from uf_gksdbclryb" );
            int index = 1;
            while (rs.next()) {
                JSONObject jsonData = new JSONObject();
                String id = Util.null2String( rs.getString( "id" ) );
                String ksdw = Util.null2String( rs.getString( "ksdw" ) );
                String sqlwhere1 = " left(sqrq,7) in ("+yearmonth1+") and zrbm='" + id + "'";
                int counts1 = getCounts( sqlwhere1 );
                String sqlwhere2 = " left(sqrq,7) in ("+yearmonth2+") and zrbm='" + id + "'";
                int counts2 = getCounts( sqlwhere2 );
                String sqlwhere3 = " left(sqrq,7) in ("+yearmonth3+") and zrbm='" + id + "'";
                int counts3 = getCounts( sqlwhere3 );
                String sqlwhere4 = " left(sqrq,7) in ("+yearmonth4+") and zrbm='" + id + "'";
                int counts4 = getCounts( sqlwhere4 );
                jsonData.put( "index", index );
                jsonData.put( "departmentname", ksdw );
                jsonData.put( "quartercount1", counts1 );
                jsonData.put( "quartercount2", counts2 );
                jsonData.put( "quartercount3", counts3 );
                jsonData.put( "quartercount4", counts4 );
                jsonArray.add( jsonData );
                index++;
            }
        }
    }else if("3".equals( type )){//月度
        if(!"0".equals(params2)){
            String month = Util.getIntValue(params2)<10?"0"+params2:params2;
            String yearmonth = params1+"-"+month;
            int index = 1;
            rs.executeQuery( "select id,ksdw from uf_gksdbclryb" );
            while (rs.next()) {
                JSONObject jsonData = new JSONObject();
                String id = Util.null2String( rs.getString( "id" ) );
                String ksdw = Util.null2String( rs.getString( "ksdw" ) );
                String sqlwhere = " left(sqrq,7)='"+yearmonth+"' and zrbm='" + id + "'";
                int counts = getCounts( sqlwhere );
                jsonData.put( "index", index );
                jsonData.put( "departmentname", ksdw );
                jsonData.put( "month"+params2, counts );
                jsonArray.add( jsonData );
                index++;
            }
        }else {
            String yearmonth1 = params1+"-01";
            String yearmonth2 = params1+"-02";
            String yearmonth3 = params1+"-03";
            String yearmonth4 = params1+"-04";
            String yearmonth5 = params1+"-05";
            String yearmonth6 = params1+"-06";
            String yearmonth7 = params1+"-07";
            String yearmonth8 = params1+"-08";
            String yearmonth9 = params1+"-09";
            String yearmonth10 = params1+"-10";
            String yearmonth11 = params1+"-11";
            String yearmonth12 = params1+"-12";
            rs.executeQuery( "select id,ksdw from uf_gksdbclryb" );
            int index = 1;
            while (rs.next()) {
                JSONObject jsonData = new JSONObject();
                String id = Util.null2String( rs.getString( "id" ) );
                String ksdw = Util.null2String( rs.getString( "ksdw" ) );
                String sqlwhere1 = " left(sqrq,7) ='"+yearmonth1+"' and zrbm='" + id + "'";
                int counts1 = getCounts( sqlwhere1 );
                String sqlwhere2 = " left(sqrq,7) ='"+yearmonth2+"' and zrbm='" + id + "'";
                int counts2 = getCounts( sqlwhere2 );
                String sqlwhere3 = " left(sqrq,7) ='"+yearmonth3+"' and zrbm='" + id + "'";
                int counts3 = getCounts( sqlwhere3 );
                String sqlwhere4 = " left(sqrq,7) ='"+yearmonth4+"' and zrbm='" + id + "'";
                int counts4 = getCounts( sqlwhere4 );
                String sqlwhere5 = " left(sqrq,7) ='"+yearmonth5+"' and zrbm='" + id + "'";
                int counts5 = getCounts( sqlwhere5 );
                String sqlwhere6 = " left(sqrq,7) ='"+yearmonth6+"' and zrbm='" + id + "'";
                int counts6 = getCounts( sqlwhere6 );
                String sqlwhere7 = " left(sqrq,7) ='"+yearmonth7+"' and zrbm='" + id + "'";
                int counts7 = getCounts( sqlwhere7 );
                String sqlwhere8 = " left(sqrq,7) ='"+yearmonth8+"' and zrbm='" + id + "'";
                int counts8 = getCounts( sqlwhere8 );
                String sqlwhere9 = " left(sqrq,7) ='"+yearmonth9+"' and zrbm='" + id + "'";
                int counts9 = getCounts( sqlwhere9 );
                String sqlwhere10 = " left(sqrq,7) ='"+yearmonth10+"' and zrbm='" + id + "'";
                int counts10 = getCounts( sqlwhere10 );
                String sqlwhere11 = " left(sqrq,7) ='"+yearmonth11+"' and zrbm='" + id + "'";
                int counts11 = getCounts( sqlwhere11 );
                String sqlwhere12 = " left(sqrq,7) ='"+yearmonth12+"' and zrbm='" + id + "'";
                int counts12 = getCounts( sqlwhere12 );
                jsonData.put( "index", index );
                jsonData.put( "departmentname", ksdw );
                jsonData.put( "month1", counts1 );
                jsonData.put( "month2", counts2 );
                jsonData.put( "month3", counts3 );
                jsonData.put( "month4", counts4 );
                jsonData.put( "month5", counts5 );
                jsonData.put( "month6", counts6 );
                jsonData.put( "month7", counts7 );
                jsonData.put( "month8", counts8 );
                jsonData.put( "month9", counts9 );
                jsonData.put( "month10", counts10 );
                jsonData.put( "month11", counts11 );
                jsonData.put( "month12", counts12 );
                jsonArray.add( jsonData );
                index++;
            }
        }
    }else if("4".equals( type )){//日期
        int index = 1;
        rs.executeQuery( "select id,ksdw from uf_gksdbclryb" );
        while (rs.next()) {
            JSONObject jsonData = new JSONObject();
            String id = Util.null2String( rs.getString( "id" ) );
            String ksdw = Util.null2String( rs.getString( "ksdw" ) );
            String sqlwhere = " sqrq>='"+params1+"' and sqrq<='"+params2+"' and zrbm='" + id + "'";
            int counts = getCounts( sqlwhere );
            jsonData.put( "index", index );
            jsonData.put( "departmentname", ksdw );
            jsonData.put( "dateinterval", counts );
            jsonArray.add( jsonData );
            index++;
        }
    }


    out.print( jsonArray.toJSONString() );
%>
<%!
    public static int getCounts(String sqlwhere) {
        RecordSet rs = new RecordSet();
        int counts = 0;
        rs.executeQuery( "select count(zrbm) FROM view_custom_bmtj where " + sqlwhere + " GROUP BY zrbm" );
        if (rs.next()) {
            counts = Util.getIntValue( rs.getString( 1 ) );
        }
        return counts;
    }
%>
<%!
    //根据当前日期判断所属季度最后一天
    public static String getDateByQuart(String year,String quarter) {
        String str = "";
        switch (quarter){
            case "1": str="'"+year+"-01','"+year+"-02','"+year+"-03'";break;
            case "2": str="'"+year+"-04','"+year+"-05','"+year+"-06'";break;
            case "3": str="'"+year+"-07','"+year+"-08','"+year+"-09'";break;
            case "4": str="'"+year+"-10','"+year+"-11','"+year+"-12'";break;
            default:str="";
        }
        return str;
    }
%>